<?php
/*
 * @Description:
 * @Author: 橘子味的心
 * @Date: 2021-01-24 17:29:03
 * @LastEditTime: 2021-01-26 18:01:10
 * @LastEditors: 橘子味的心
 * @Reference:
 */
namespace app\system\model;

use think\Model;

class Attachment extends Model
{
    protected $append = [
        'storage_text',
        'url_text',
    ];
    /**
     * 根据条件获取配置列表信息
     */
    public function getAll($limit, $start_time = false, $end_time = false, $key = false, $map = [])
    {
        if (!empty($start_time) && empty($end_time)) {
            $star_time = strtotime($start_time);
            $map[] = ['create_time', 'between', [$star_time, time()]];
        }
        if (!empty($start_time) && !empty($end_time)) {
            $star_time = strtotime($start_time);
            $end_time = strtotime($end_time);
            $map[] = ['create_time', 'between', [$star_time, $end_time]];
        }
        if (!empty($key)) {
            $map[] = ['name', 'like', "%" . $key . "%"];
        }
        $res = $this
            ->where($map)
            ->order('create_time desc')
            ->paginate($limit)
            ->each(function ($item) {
                $item['filesize'] = format_bytes($item['filesize']);
                return $item;
            });
        return $res;
    }
    /**
     * 根据条件获取配置列表信息
     */
    public function getAlls($where = [], $map = [])
    {
        if (!empty($where['key'])) {
            if ($where['key'] == 'other') {
                $map[] = ['mimetype', 'NOT LIKE', "image%"];
                $map[] = ['mimetype', 'NOT LIKE', "video%"];
            } else {
                $map[] = ['mimetype', 'like', $where['key'] . "%"];
            }
        }
        if(!empty($where['group_id']) && (int) $where['group_id'] !== 0){
            $map[] = ['group_id', '=', $where['group_id']];
        }
        $res = $this
            ->where($map)
            ->append(['url_text'])
            ->order('create_time desc')
            ->paginate($where['limit'])
            ->each(function ($item) {
                $item['filesize'] = format_bytes($item['filesize']);
                return $item;
            });
        return $res;
    }
    /**
     * 添加/编辑信息
     */
    public function info($param)
    {
        if (!empty($param['id'])) {
            $res = $this::find($param['id']);
        } else {
            $res = $this;
        }
        try {
            $res->save($param);
        } catch (\Exception $e) {
            return false;
        }
        return true;
    }
    /**
     * 根据id获取配置信息
     * @param $id
     */
    public function getOne($id)
    {
        return $this->where('id', $id)->find();
    }

    /**
     * 状态切换
     */
    public function status($id, $status)
    {
        try {
            $this->where('id', $id)->update(['status' => $status]);
        } catch (\Exception $e) {
            return false;
        }
        return true;
    }
    /**
     * 批量操作
     */
    public function handleAll($param, $type = 0)
    {
        try {
            //判断是否是批量删除
            if ($type == 2) {
                $data = $this->where('id', 'in', $param)->select();
                foreach ($data as $k => $v) {
                    $file = root_path('public') . 'upload/' . $v['url'];
                    if (is_file($file)) {
                        unlink($file);
                    }
                    $v->delete();
                }
            } else {
                $this->where('id', 'in', $param)->update(['status' => $type]);
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
    public function getUrlTextAttr($val, $data)
    {
        return empty($data['file_id']) ? '' : _sImage($data['file_id']);
    }
    public function getStorageTextAttr($val, $data)
    {
        switch ($data['storage']) {
            case '0':
                $txt = '本地';
                break;
            case '1':
                $txt = '七牛云';
                break;
            case '2':
                $txt = '阿里云';
                break;
            case '3':
                $txt = 'SMMS图床';
                break;
            default:
                $txt = '其他';
                break;
        }
        return $txt;
    }
}
